{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 问题\n",
    "\n",
    "通过 date 命令查看时间\n",
    "\n",
    "- 查看主机时间\n",
    "\n",
    "```sh\n",
    "[root@localhost ~]# date\n",
    "2016年 07月 27日 星期三 22:42:44 CST\n",
    "```\n",
    "\n",
    "- 查看容器时间\n",
    "\n",
    "```sh\n",
    "root@b43340ecf5ef:/#date                     \n",
    "Wed Jul 27 14:43:31 UTC 2016\n",
    "```\n",
    "\n",
    "可以发现，他们相隔了 8 小时。\n",
    "\n",
    "- CST 应该是指（China Shanghai Time，东八区时间）   \n",
    "- UTC 应该是指（Coordinated Universal Time，标准时间） \n",
    "\n",
    "所以，这 2 个时间实际上应该相差 8 个小时。(bluer: 所以没有设置过的容器, 一般跟宿主机时间相差 8h)\n",
    "\n",
    "所以，必须统一两者的时区。\n",
    "\n",
    "### 共享主机的 localtime  (方法一)\n",
    "\n",
    "创建容器的时候指定启动参数，挂载 localtime 文件到容器内  ，保证两者所采用的时区是一致的。\n",
    "\n",
    "```sh\n",
    "docker run -d -v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime --rm -p 8888:8080 tomcat:latest\n",
    "```\n",
    "\n",
    "### 复制主机的 localtime  (方法二)\n",
    "\n",
    "```sh\n",
    "docker cp /usr/share/zoneinfo/Asia/Shanghai 【容器ID或者NAME】:/etc/localtime\n",
    "```\n",
    "\n",
    "在完成后，再通过 date 命令进行查看当前时间。 \n",
    "\n",
    "但是，在容器中运行的程序的时间不一定能更新过来，比如在容器运行的 MySQL 服务，在更新时间后，通过 sql 查看 MySQL 的时间\n",
    "\n",
    "```sql\n",
    "select now() from dual;\n",
    "\n",
    "```\n",
    "\n",
    "可以发现，时间并没有更改过来。 \n",
    "\n",
    "这时候必须要重启 mysql 服务或者重启 Docker 容器，mysql 才能读取到更改过后的时间。\n",
    "\n",
    "### 创建自定义的 dockerfile  (方法三)\n",
    "\n",
    "创建 dockerfile 文件，其实没有什么内容，就是自定义了该镜像的时间格式及时区。\n",
    "\n",
    "```sh\n",
    "...\n",
    "\n",
    "#定义时区参数\n",
    "ENV TZ=Asia/Shanghai\n",
    "\n",
    "#设置时区\n",
    "RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone\n",
    "\n",
    "...\n",
    "```\n",
    "\n",
    "保存后，利用 docker build 命令生成镜像使用即可。\n",
    "\n",
    "\n",
    "**在 docker 容器和系统时间不一致是因为 docker 容器的原生时区为 0 时区，而国内系统为东八区，当然，我们不深究其原因。**\n",
    "\n",
    "1）进入容器\n",
    "\n",
    "```\n",
    "docker exec -t -i c360cc412528 /bin/bash    // c360cc412528 为容器id\n",
    "\n",
    "```\n",
    "\n",
    "2）在 / usr/share/zoneinfo 目录下找上海时区\n",
    "\n",
    "```\n",
    "cd  /usr/share/zoneinfo/Asia\n",
    "\n",
    "```\n",
    "\n",
    "3）复制上海时区到 /etc 重命名 localtime 文件\n",
    "\n",
    "```\n",
    "copy Shanghai /etc/localtime \n",
    "\n",
    "```\n",
    "\n",
    "在使用 copy 时，提示我们：\n",
    "\n",
    "bash: copy: command not found.\n",
    "\n",
    "那么我们可以使用 cp -i Shanghai /etc/localtime, 效果是一样的\n",
    "\n",
    "完成后的效果\n",
    "\n",
    "![](https://img-blog.csdn.net/20180831144039456?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3Mjk1NDAz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "288px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
